package cn.chinaflame.pagodas.pool;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;

import static cn.chinaflame.pagodas.constant.Sysconst.PILLARS;
import static cn.chinaflame.pagodas.constant.Sysconst.MAXPILLARS;
import static cn.chinaflame.pagodas.constant.Sysconst.MINPILLARS;
public class ThreadPool
{

	private static final int maxIdleTime = 5;//最大空闲时间5秒
	/**
	 * 
	 */
	public static ExecutorService executorFlashPool = Executors.newFixedThreadPool(100, new ThreadFactory()
	{
		@Override
		public Thread newThread(Runnable r)
		{
				Thread thread = Executors.defaultThreadFactory().newThread(new Thread(r));
				thread.setName("[executorFlashPool]-" + thread.getName());
//				thread.setDaemon(true);
				return thread;
		}
	});
	
	/**
	 * 接收到牌局报文后放入队列，通过这个线程池消费。
	 */
	public static ExecutorService executorRoundResultPool = Executors.newFixedThreadPool(500, new ThreadFactory()
	{
		@Override
		public Thread newThread(Runnable r)
		{
			{
				Thread thread = Executors.defaultThreadFactory().newThread(new Thread(r));
				thread.setName("[executorRoundResultPool]-" + thread.getName());
//				thread.setDaemon(true);
				return thread;
			}
		}
	});
	
	/**
	 * 应用中使用的定时任务线程池
	 */
	public static ScheduledExecutorService scheduledExecutorService_resumeThread = Executors.newScheduledThreadPool(10, new ThreadFactory()
	{
		@Override
		public Thread newThread(Runnable r)
		{
			{
				Thread thread = Executors.defaultThreadFactory().newThread(new Thread(r));
				thread.setName("[scheduledExecutorService_resumeThread]-" + thread.getName());
//				thread.setDaemon(true);
				return thread;
			}
		}
	});
	
	/**
	 * 执行其他任务的线程
	 */
	public static ExecutorService executorResumePool = Executors.newCachedThreadPool();
	
	/**        
	 * 执行其他任务的线程
	 */
	public static ExecutorService executorOtherPool = Executors.newFixedThreadPool(50, new ThreadFactory(){
		@Override
		public Thread newThread(Runnable r)
		{
			{
				Thread thread = Executors.defaultThreadFactory().newThread(new Thread(r));
				thread.setName("[executorOtherPool]-" + thread.getName());
//				thread.setDaemon(true);
				return thread;
			}
		}
	});
	
	public static final String SEARCHTHREAD = "searchthread";//自定义寻灯线程各前缀
	public static final String SENDTHREAD = "sendthread";//自定义安灯线程各前缀
	public static final String CHECKTHREAD = "checkthread";//自定义检查灯位线程各前缀
	public static final String CLEARTHREAD = "clearthread";//自定义清灯线程各前缀
	
	final static ThreadFactory SEARCHTHREADFACTORY = new ThreadFactoryBuilder()    //自定义寻灯线程工厂
	        .setNameFormat("[" + SEARCHTHREAD + "-%d]" ) 
	        .setPriority(Thread.MAX_PRIORITY)
	        .build();  
	final static ThreadFactory SENDTHREADFACTORY = new ThreadFactoryBuilder()  //自定义安灯线程工厂
			.setPriority(Thread.MAX_PRIORITY)
	        .setNameFormat("[" + SENDTHREAD + "-%d]" )  
	        .build(); 
	final static ThreadFactory CHECKTHREADFACTORY = new ThreadFactoryBuilder()  //自定义检查灯位线程工厂
			.setPriority(Thread.MAX_PRIORITY)
	        .setNameFormat("[" + CHECKTHREAD + "-%d]")  
	        .build(); 
	final static ThreadFactory CLEARTHREADFACTORY = new ThreadFactoryBuilder()  //自定义清灯线程工厂
			.setPriority(Thread.MAX_PRIORITY)
	        .setNameFormat("[" + CLEARTHREAD + "-%d]")  
	        .build(); 
	//线程拒绝策略，重新焕起
	private static final RejectedExecutionHandler defaultHandler = new ThreadPoolExecutor.CallerRunsPolicy();
	//寻灯线程池，默认最小和最大线程为每层楼最大的墙数，线程空闲时间为5秒，等待队列为空
	public static final ExecutorService SEARCHPOOL = new ThreadPoolExecutor(MAXPILLARS,MAXPILLARS, maxIdleTime, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(), SEARCHTHREADFACTORY, defaultHandler);
	//安灯线程池，默认最小和最大线程为每层楼最大的墙数，线程空闲时间为5秒，等待队列为空
	public static final ExecutorService SENDPOOL =  Executors.newWorkStealingPool();
//	new ThreadPoolExecutor(MAXPILLARS,MAXPILLARS, maxIdleTime, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(), SENDTHREADFACTORY, defaultHandler);
	//检查灯位线程池，默认最小和最大线程为每层楼最大的墙数，线程空闲时间为5秒，等待队列为空
	public static final ExecutorService CHECKPOOL =  Executors.newWorkStealingPool();
//	new ThreadPoolExecutor(MAXPILLARS,MAXPILLARS, maxIdleTime, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(), CHECKTHREADFACTORY, defaultHandler);
	//清灯线程池，默认最小和最大线程为每层楼最大的墙数，线程空闲时间为5秒，等待队列为空
	public static final ExecutorService CLEARPOOL =  Executors.newWorkStealingPool();
//	new ThreadPoolExecutor(MAXPILLARS,MAXPILLARS, maxIdleTime, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(), CLEARTHREADFACTORY, defaultHandler);

}
